package com.yc.test.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Date: 2018/11/30 16:21
 * @Author: Can.Yang
 * @Description:
 */
public class Stream {

    public static void main(String[] args) {
        stream();
    }


    public static void stream() {

        List<Person> list = new ArrayList<Person>() {{
            add(new Person(1, "YA", 20, "男"));
            add(new Person(2, "YB", 24, "女"));
            add(new Person(3, "YC", 43, "男"));
            add(new Person(4, "YD", 32, "女"));
            add(new Person(5, "YE", 23, "女"));
            add(new Person(6, "YF", 23, "男"));
            add(new Person(7, "YG", 25, "女"));
            add(new Person(8, "YH", 29, "男"));
            add(new Person(9, "YI", 20, "男"));
            add(new Person(10, "YJ", 32, "女"));
            add(new Person(11, "YK", 20, "男"));
        }};

        //java 流
        //并行流：parallelStream()
        //普通流：stream()

        System.out.println("********************循环*********************");
        list.forEach(d -> {
            System.out.println(d);
        });

        System.out.println("********************过滤，返回特定格式数据*********************");
        List<String> nameList = list.stream().filter(d -> d.getAge() > 30 && d.getSex().equals("男"))
                .map(d -> d.getName()).collect(Collectors.toList());
        System.out.println(nameList);

        System.out.println("********************判断列表中是否包含满足条件的元素*********************");
        if (list.stream().anyMatch(d -> d.getAge() == 22)) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }

        System.out.println("********************判断列表中是否全部满足条件*********************");
        if (list.stream().allMatch(d -> d.getAge() > 18)) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }

        System.out.println("********************查询第一个匹配元素，没有则返回null*********************");
        Person person = list.stream().filter(d -> d.getAge() > 30 && d.getSex().equals("男"))
                .findFirst().orElse(null);
        System.out.println(person);

        System.out.println("********************去重*********************");
        List<String> sexList = list.stream().map(d -> d.getSex()).distinct().collect(Collectors.toList());
        System.out.println(sexList);

        System.out.println("********************跳过2条数据，显示3条*********************");
        list.stream().skip(2).limit(3).forEach(d -> System.out.println(d));

        System.out.println("********************排序:正序*********************");
        list.stream().filter(d -> d.getAge() == 20)
                .sorted(Comparator.comparing(Person::getId)).forEach(d -> System.out.println(d));

        System.out.println("********************排序:倒序*********************");
        list.stream().filter(d -> d.getAge() == 20)
                .sorted(Comparator.comparing(Person::getId).reversed()).forEach(d -> System.out.println(d));

        System.out.println("********************求和,Count*********************");
        Integer ageSum = list.stream().map(Person::getAge).reduce(0, Integer::sum);
        System.out.println(ageSum);
        Long count = list.stream().filter(d -> d.getAge() > 30).count();
        System.out.println(count);

        System.out.println("********************年龄最大，年龄最小*********************");
        Integer minAge = list.stream().mapToInt(Person::getAge).min().getAsInt();
        System.out.println(minAge);
        Integer maxAge = list.stream().mapToInt(Person::getAge).max().getAsInt();
        System.out.println(maxAge);

        System.out.println("********************查询年龄和此年龄的人数*********************");
        Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.counting()));
        System.out.println(map);

        System.out.println("********************查询年龄和人员列表*********************");
        Map<Integer, List<Person>> map1 = list.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.toList()));
        System.out.println(map1);

        System.out.println("********************拼接字符串*********************");
        String names = list.stream().map(d -> d.getName()).collect(Collectors.joining(",", "{", "}"));
        System.out.println(names);

    }
}
